//
//  PhotoSlideShow.m
//

#import "PhotoSlideShow.h"

#import "Controller.h"
#import "PreferencesData.h"
#import "SlideshowModel.h"
#import "ThumbData.h"
#import "Utility.h"

#import "QTKit/QTKit.h"


@implementation PhotoSlideShow

//------------------------------------------------------------------------------
- (void) awakeFromNib
{
	[super awakeFromNib];
	[[imageView window] setTitle: @"Slideshow"];

	showIndex = YES;
	showImageSize = NO;
	showFileLength = NO;
	showDate = YES;
	showName = YES;

	[self setThumbViewState: TVS_HIDDEN];
	fitWindow = YES;

	playing = NO;

	[self updateColors];
}

//------------------------------------------------------------------------------
- (void) dealloc
{
	[ssModel release];
	[tdCurrentFile release];

	[super dealloc];
}

//------------------------------------------------------------------------------
- (ThumbData*) getNextFile
{
	NSString *filename = [ssModel nextFile];
	ThumbData *td =[[ThumbData alloc] initWithFilename: filename];
	[td setExifDate: exifOrFileDate(filename)];
	return td;
}

//------------------------------------------------------------------------------
- (ThumbData*) getPreviousFile
{
	NSString *filename = [ssModel previousFile];
	ThumbData *td =[[ThumbData alloc] initWithFilename: filename];
	return td;
}

//------------------------------------------------------------------------------
- (void) setModel:(SlideshowModel*) model  start:(BOOL) start
{
	[ssModel release];
	ssModel = model;
	[ssModel retain];

	if (YES == start)
	{
		playing = NO;
		[self toggleAutoShow: nil];
	}
}

//------------------------------------------------------------------------------
- (void) redrawCurrent
{
}

//------------------------------------------------------------------------------
- (void) prefsDataChanged:(NSNotification*) note
{
	pathElements = [[[Controller sharedController] preferencesData] pathElements];
	[self updateColors];
	[super prefsDataChanged: note];
}

//------------------------------------------------------------------------------
- (void) updateColors
{
	NSColor *backgroundColor = [[[Controller sharedController] preferencesData] slideshowBackgroundColor];

	if (nil != backgroundColor)
	{
		[imageView setBackgroundColor: backgroundColor];
		[[imageView enclosingScrollView] setBackgroundColor: backgroundColor];
		[[thumbView enclosingScrollView] setBackgroundColor: backgroundColor];
		[movieView setFillColor: backgroundColor];
		[[imageView window] setBackgroundColor: backgroundColor];
	}
	else
	{
		[imageView setBackgroundColor: originalColor];
		[[imageView enclosingScrollView] setBackgroundColor: originalColor];
		[[thumbView enclosingScrollView] setBackgroundColor: originalColor];
		[movieView setFillColor: originalColor];
		[[imageView window] setBackgroundColor: originalColor];
	}

	NSColor *textColor = [[[Controller sharedController] preferencesData] slideshowTextColor];
	if (nil != textColor)
	{
		[imageView setForegroundColor: textColor];
		[thumbView setForegroundColor: textColor];
	}
	else
	{
		[imageView setForegroundColor: nil];
		[thumbView setForegroundColor: nil];
	}
}

//------------------------------------------------------------------------------
- (IBAction) nextImage:(id)sender
{
	[self cancelAutoShow];

	[tdCurrentFile release];
	tdCurrentFile = [self getNextFile];
 	[tdCurrentFile retain];

	[self displayCurrentFile];
}

//------------------------------------------------------------------------------
- (IBAction) previousImage:(id)sender
{
	[self cancelAutoShow];

	[tdCurrentFile release];
	tdCurrentFile = [self getPreviousFile];
 	[tdCurrentFile retain];

	[self displayCurrentFile];
}

//------------------------------------------------------------------------------
- (IBAction) toggleAutoShow:(id)sender
{
	if (YES == playing)
	{
		[self cancelAutoShow];
	}
	else
	{
		disallowSleepTimer = [NSTimer 
			scheduledTimerWithTimeInterval: 30
			target: self
			selector: @selector(disallowSleep:)
			userInfo: nil
			repeats: YES];

		playing = YES;
		[self performSelector: @selector(nextAutoShow:)  withObject: nil  afterDelay: 0.0];
	}
}

//------------------------------------------------------------------------------
- (void) nextAutoShowTimer:(NSTimer*) timer
{
	if (YES == playing)
	{
		[self nextAutoShow: nil];
	}
}

//------------------------------------------------------------------------------
- (void) disallowSleep:(NSTimer*) timer
{
	// Keep the screen saver from running
	UpdateSystemActivity(UsrActivity);
}

//------------------------------------------------------------------------------
- (void) nextAutoShow:(id) notta
{

	[NSCursor setHiddenUntilMouseMoves: YES];

	[tdCurrentFile release];
	tdCurrentFile = [self getNextFile];
 	[tdCurrentFile retain];
	[self displayCurrentFile];
}

//------------------------------------------------------------------------------
- (void) displayCurrentFile
{
	[self displayFile: tdCurrentFile];
	[super setImageStrings
		: [tdCurrentFile fullPath]  
		index:[ssModel index] + 1  
		totalCount:[ssModel fileCount]  
		date:[tdCurrentFile date]];

	if (YES == playing)
	{
		if (MEDIA_TYPE_MOVIE != [tdCurrentFile mediaType])
		{
			// Create a one shot timer for images - movies will autoplay and call
			// back here when they're done.
			[NSTimer 
				scheduledTimerWithTimeInterval: [ssModel duration]
				target: self
				selector: @selector(nextAutoShowTimer:)
				userInfo: nil
				repeats: NO];
		}
	}
}

//------------------------------------------------------------------------------
- (void) cancelAutoShow
{
	if (YES == playing)
	{
		playing = NO;
		[disallowSleepTimer invalidate];
		disallowSleepTimer = nil;
	}
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
- (BOOL) validateMenuItem:(id <NSMenuItem>) menuItem
{
	BOOL enabled = NO;
	[super validateMenuItem: menuItem];

	switch ([menuItem tag] & MN_TAG_MASK)
	{
		case MN_TAG_ALWAYS:
			enabled = YES;
			break;
		case MN_TAG_PATH:
			enabled = nil != ssModel;
			break;
		case MN_TAG_IMAGE:
			enabled = nil != ssModel && 0 != [ssModel fileCount];
			break;
		case MN_TAG_JPEG:
NSLog(@"PSS.validateMenuItem %@", menuItem);
//			enabled = [self hasSelectedJPEG];
			break;
	}

	// Setup the autoshow menu
	if ([menuItem action] == @selector(toggleAutoShow:))
	{
		NSString *menuCaption;
		if (YES == playing)
		{
			menuCaption = NSLocalizedString(@"Stop Slideshow", @"menu.stopSlideshow");
		}
		else
		{
			menuCaption = NSLocalizedString(@"Start Slideshow", @"menu.startSlideshow");
		}

		[menuItem setTitle: menuCaption];
	}

	return enabled;
}

//------------------------------------------------------------------------------
- (void) movieDidEndNotification:(NSNotification*) note
{
	if (YES == playing && nil != tdCurrentFile)
	{
		QTMovie *movie = [note object];

		[[NSNotificationCenter defaultCenter] removeObserver
				:self
				name: QTMovieDidEndNotification
				object: movie];

		NSString *name = [[movie movieAttributes] objectForKey: QTMovieFileNameAttribute];

		if (NSOrderedSame == [[tdCurrentFile fullPath] caseInsensitiveCompare: name])
		{
			[self nextAutoShow: nil];
		}
	}
}

//------------------------------------------------------------------------------
- (void) movieDisplayed:(QTMovie*) movie
{
	if (YES == playing)
	{
		[[NSNotificationCenter defaultCenter] addObserver 
				:self
				selector:@selector(movieDidEndNotification:)
				name: QTMovieDidEndNotification
				object: movie];

		[movie play];
	}
}

//------------------------------------------------------------------------------
- (void) windowWillClose:(NSNotification*) notification
{
	[self cancelAutoShow];
	[self endFullScreenDuringClose];

	// Cancel any movie currently playing.
	[movieView pause: nil];
	[movieView setMovie: nil];

	NSUserDefaults *userPrefs = [[Controller sharedController] userPreferences];
	NSRect frame = [[imageView window] frame];
	[userPrefs setObject: NSStringFromRect(frame)  forKey: PREF_SLIDESHOW_FRAME_RECT];

	[[Controller sharedController] slideshowClosing: self];
}

@end
